﻿using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Input;
using System.Collections.Generic;

namespace SBPweb.PROvibe.Client.Framework.Controls
{

    public partial class ContextMenu : ContentControl
    {

        #region Private fields

        private ContextMenuCatcher cmc;
        private Popup popup;

        #endregion

        #region Constructor logic

        public ContextMenu()
        {
            DefaultStyleKey = typeof(ContextMenu);
            this.Style = Application.Current.Resources["NormalContextMenu"] as Style;
        }

        #endregion

        #region Property accessors

        #endregion

        #region Methods

        private void Initialize()
        {
            popup = this.GetTemplateChild("ContextMenuPopup") as Popup;
            InitializeCatcher(Parent as UIElement);
        }

        public void InitializeCatcher(UIElement relativeElement)
        {
            if (cmc != null)
            {
                cmc.Click -= new EventHandler<HtmlClickEventArgs>(ContextMenuCatcher_Click);
            }
            cmc = new ContextMenuCatcher(relativeElement);
            cmc.Click += new EventHandler<HtmlClickEventArgs>(ContextMenuCatcher_Click);
            cmc.ContextExpand += new EventHandler<HtmlClickEventArgs>(ContextMenuCatcher_ContextExpand);
            cmc.ContextCollapse += new EventHandler(ContextMenuCatcher_ContextCollapse);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Initialize();
        }

        private void ContextMenuCatcher_Click(object sender, HtmlClickEventArgs e)
        {
            if (popup == null)
            {
                return;
            }
            switch (e.MouseButton)
            {
                default:
                break;
                case MouseButtons.Left:
                    popup.IsOpen = false;
                break;
            }
        }

        private void ContextMenuCatcher_ContextExpand(object sender, HtmlClickEventArgs e)
        {
            if (popup == null)
            {
                return;
            }
            popup.HorizontalOffset = e.RelativeLocation.X;
            popup.VerticalOffset = e.RelativeLocation.Y;
            popup.IsOpen = true;
        }

        private void ContextMenuCatcher_ContextCollapse(object sender, EventArgs e)
        {
            if (popup == null)
            {
                return;
            }
            popup.IsOpen = false;
        }

        #endregion

        #region Subclasses

        //internal class ContextMenuCatcher
        //{

        //    private static AncestorTree<FrameworkElement> tree = new AncestorTree<FrameworkElement>();
        //    protected UIElement control;
        //    private bool isMouseHover;

        //    internal event EventHandler<HtmlClickEventArgs> Click;

        //    internal ContextMenuCatcher(ContextMenu contextMenu, UIElement control)
        //    {
        //        this.control = control;
        //        this.control.MouseEnter += new MouseEventHandler(OnControlMouseEnter);
        //        this.control.MouseLeave += new MouseEventHandler(OnControlMouseLeave);
        //        HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);
        //        HtmlPage.Document.AttachEvent("onmousedown", OnMouseDown);
        //        HtmlPage.Document.AttachEvent("onmousewheel", OnMouseWheel);
        //        //AncestorTreeNode<FrameworkElement> atn = new AncestorTreeNode<FrameworkElement>(contextMenu as FrameworkElement);
        //        //tree.MergeNodes(atn);
        //        //var node = tree.FindElementRecursively<string>("cmVideoPopup", new AncestorTree<FrameworkElement>.ElementMemberComparison<string>(s));
        //    }

        //    private bool s(AncestorTreeNode<FrameworkElement> atn, string s)
        //    {
        //        return atn.Element.Name == s;
        //    }

        //    internal UIElement RelativeControl
        //    {
        //        get
        //        {
        //            return control;
        //        }
        //        set
        //        {
        //            control = value;
        //        }
        //    }

        //    private void OnMouseDown(object sender, HtmlEventArgs e)
        //    {
        //        if(e.MouseButton == MouseButtons.Left && Click != null)
        //        {
        //            Click(this, new HtmlClickEventArgs(control, e));
        //        }
        //    }

        //    private void OnMouseWheel(object sender, HtmlEventArgs e)
        //    {
        //        object c = e.EventObject.GetProperty("wheelDelta");
        //        if (c != null)
        //        {
        //            int delta = int.Parse(c.ToString());
        //            if (delta > 0)
        //            {
        //                //++
        //            }
        //            else
        //            {
        //                //--
        //            }
        //        }
        //    }

        //    private void OnContextMenu(object sender, HtmlEventArgs e)
        //    {
        //        e.PreventDefault();
        //        e.StopPropagation();
        //        if (Click != null && CheckIfControlIsHovered())
        //        {
        //            Click(this, new HtmlClickEventArgs(control, e));
        //        }
        //    }
            
        //    private void OnControlMouseEnter(object sender, MouseEventArgs e)
        //    {
        //        isMouseHover = true;
        //    }

        //    private void OnControlMouseLeave(object sender, MouseEventArgs e)
        //    {
        //        isMouseHover = false;
        //    }

        //    private bool CheckIfControlIsHovered()
        //    {
        //        Find();
        //        return isMouseHover;// && (stack.Peek() == control);
        //    }

        //    private void Find()
        //    {
        //        FrameworkElement fE = control as FrameworkElement;
        //        return;
        //    }

        //}

        #endregion

    }

    public class HtmlClickEventArgs : EventArgs
    {

        public HtmlClickEventArgs(UIElement control, HtmlEventArgs e)
        {
            Point mouseLocation = new Point(e.OffsetX, e.OffsetY);
            this.Control = control;
            this.AbsoluteLocation = mouseLocation;
            this.RelativeLocation = GetRelativeLocation(control, mouseLocation);
            this.MouseButton = e.MouseButton;
            if (e.EventType.ToLower() == "contextmenu")
            {
                this.MouseButton = MouseButtons.Right;
            }
        }

        public UIElement Control;

        public Point RelativeLocation;

        public Point AbsoluteLocation;

        public MouseButtons MouseButton;

        private Point GetRelativeLocation(UIElement element, Point point)
        {
            GeneralTransform transform = Application.Current.RootVisual.TransformToVisual(element);
            return transform.Transform(point);
        }


    }

}
